Linking To ClientProtector Using Microsoft C#.NET

SoftwareShield ClientProtector COM Component > Linking To ClientProtector In Your Application > Linking To ClientProtector™ Using Microsoft C#.NET®

## Linking To ClientProtector™ Using Microsoft C#.NET®

Please be sure you have read Linking To The ClientProtector COM Component In Your Application before you perform this procedure.

If you run into trouble, you may prefer to start with the Sample Code. There are a set of three complete compile-able C#.NET programs available.

The following example was prepared and tested in Microsoft Visual Studio .NET 2003 in the C# language.

The method we use here involves using the interoperability mechanism available to .NET applications to use the ClientProtector COM server just like it was another object in our application. This is a breeze in MSVC#. For additional information you may wish to see the MSDN page "Calling COM Components from .NET Clients".

1. Open Microsoft Visual Studio .NET. Open a new or existing C# project to which you want to use the ClientProtector.
2. In the Solution Explorer window select the project and right-click it and select "Add Reference". (You can also do this from the "Project" menu.)
3. The "Add References" dialog will appear. Select the "COM" tab. in the component grid, find the "SoftwareShield ClientProtector" and select it. Click the "Select" button. It will appear in the "Selected Components" grid at the bottom of the dialog. Click "OK". If you have the debug version registered it will say "SoftwareShield DEBUG ClientProtector" - that's OK. The dialog is shown below:
4. In the main form of your application, insert the "using" directive to use the imported ClientProtector namespace, like this:
   using SSCProt;
5. In the main form of your application, declare a member of the ClientProtector type like this:
   // the ClientProtector server
   private SSCProtectorClass SSCP;
6. In the Form-Load event of the main form, create the object with a call to new. As well, connect the event handlers you need by simply typing in the object name, event handler name, the += operator. The code completion will display the prototype for connecting a delegate event handler. Simply press "Tab" twice and the statement will be completed and the event handler will be declared. All that looks like this:
   const string MainLicenseFileName = "CPFeaturesSample.ini";
   const string MainLicenseFilePassword = "austrian_6_abode_._epitome";
   const string GlobalAuthorizationCodePassword = "featuring_CAMPANILE_2_newscast";
   const int FingerPrintOptionsCode = 6553703;
   int return_code, debugFlags = 0;
   try
   {
   SSCP = new SSCProtectorClass();
   SSCP.OnAttemptDeactivate += new
   ISSCProtectorEvents_OnAttemptDeactivateEventHandler(SSCP_OnAttemptDeactivate);
   SSCP.OnAttemptReleaseCP += new
   ISSCProtectorEvents_OnAttemptReleaseCPEventHandler(SSCP_OnAttemptReleaseCP);
   SSCP.OnMissingLicenseFile += new
   ISSCProtectorEvents_OnMissingLicenseFileEventHandler(SSCP_OnMissingLicenseFile);
   }
   catch(Exception)
   {
   MessageBox.Show("Unable to connect to license server. Please contact vendor");
   Application.Exit();
   return;
   }
   SSCP.StartUp(MainLicenseFileName,
   MainLicenseFilePassword,
   GlobalAuthorizationCodePassword,
   FingerPrintOptionsCode,
   debugFlags,
   out return_code);

When you use the "#using SSCProt" directive, the IDE also has visibility to the enumerated constants defined in RETURNCODES and CPDEBUGFLAGS (inside COM server). You have access to these defined constants directly.
Important Note About .NET FPU (Floating Point Unit) Control Word

The .NET framework expects the floating point control word to mask all exceptions internally. However the SoftwareShield COM servers expect (and must have) a standard FPU exception mask (during load and construction).

If you are having strange exceptions that disappear when you comment out the creation of your SoftwareShield COM servers, you may need to use the following simple work-around to get and reset the default control word. This is illustrated as well inside each of the C# sample programs in this DRM Kit. This should only have to be done immediately after the COM servers construction call.

1) Import the _controlFP function from the msvcrt.dll using InteropServices:

[System.Runtime.InteropServices.DllImport("msvcrt.dll")]
public static extern int _controlfp(int IN_New, int IN_Mask);

2) Create a member variable to store the default control word for your application:

private int DefaultCW;

3) During construction of your main form (or module) get the default control word and store it:

DefaultCW = _controlfp(0, 0);

4) Immediately after your call to construct the ClientProtector, reset the default control word:

SSCP = new SSCProtectorClass();

// restore the FPU control word .NET expects
_controlfp(DefaultCW, 0xfffff);

You should only have to restore the default control word once (or at least only after you create an instance of the ClientProtector).